/*
 * This file is Copyright 2002-2006 Loyola University Chicago,
 * Department of Computer Science and the Emerging Technologies Laboratory.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License via
 *    http://www.apache.org/licenses/LICENSE-2.0.
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.citep.web.admin.transactions.client;

import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.HasHorizontalAlignment;
import com.google.gwt.user.client.ui.ClickListener;
import com.google.gwt.user.client.ui.Widget;
import com.citep.web.gwt.widgets.EditableTable;
import com.citep.web.gwt.widgets.DynaTableDataProvider;
import com.citep.web.gwt.widgets.AccountSelectionWidget;
import com.citep.web.gwt.widgets.AccountTextField;
import com.citep.web.gwt.widgets.EditableTableListener;
import com.citep.web.gwt.dto.AccountDTO;


public class ImportTransactionsPanel extends Composite implements DynaTableDataProvider,EditableTableListener {

	private VerticalPanel mainPanel;

	private HorizontalPanel accountSelection;
	private EditableTable transactionTable;

	private int statementNumber;
	private ImportedStatementProvider importProvider;
	private AccountSelectionWidget accountList;
	private String importedAccountName;
	private AccountTextField accountTextField;
	
	public ImportTransactionsPanel(int statementNumber, String importedAccountName, ImportedStatementProvider importProvider) {
		
		//keeps an 'id' of the statement, in case the user imported
		//several statements at the same time
		this.statementNumber = statementNumber;
		this.importProvider = importProvider;
		this.importedAccountName = importedAccountName;
		initWidget(getMainPanel());
		transactionTable.refresh();
	}

	public void updateRowData(int startRow, int maxRows, RowDataAcceptor acceptor) {
		importProvider.updateRowData(statementNumber, startRow, maxRows, acceptor);
	}	
	
	private VerticalPanel getMainPanel() {
		if(mainPanel == null) {
			mainPanel = new VerticalPanel();
			mainPanel.add(getAccountSelection());

			mainPanel.setHorizontalAlignment(HasHorizontalAlignment.ALIGN_CENTER);

			Button submit = new Button("Submit");
			submit.setStyleName("form_big_button");
			submit.addClickListener(new ClickListener() {
				public void onClick(Widget sender) {
					AccountDTO destAccount = accountList.getSelectedAccount();
					if(destAccount != null) {
						importProvider.commitStatement(statementNumber, destAccount);
					} else {
						Window.alert("Please select a valid account");
					}
				}
			});
			
			mainPanel.add(submit);

			mainPanel.add(getTransactionTable());
		}
		
		return mainPanel;
	}
	
	private HorizontalPanel getAccountSelection() {
		if(accountSelection == null) {
			accountSelection = new HorizontalPanel();
			accountSelection.setSpacing(5);
			
			Label instructions = new Label("Please select an account which is equivalent to " + importedAccountName + ":");
			instructions.addStyleName("instructions_label");
			accountSelection.add(instructions);
			
			accountList = new AccountSelectionWidget();
			accountList.init();
			accountSelection.add(accountList);
		}
		
		return accountSelection;
	}

	private EditableTable getTransactionTable() {
		if(transactionTable == null) {			
			transactionTable = new EditableTable(this,
					new String [] {"Date", "Memo", "Source", "Debit", "Credit"},
					"data_grid",
					"data_grid_header",
					"data_grid_entry",
					new String[] {"grid_normal_item", "grid_small_item", "grid_normal_item","grid_small_item","grid_small_item"},
					10);
			
			//make all account fields editable
			//AccountTextField accountTextField = new AccountTextField("Text","account_text_label","account_text_textbox","account_text_suggest_table","account_text_suggest_hightlight");
			accountTextField = new AccountTextField("","account_text_textbox","account_text_suggest_table","account_text_suggest_hightlight");
			transactionTable.setColunmWidget(2, accountTextField);
			transactionTable.setEditableTableListener(this);
		}
		return transactionTable;
	} 

	public void onCellChanged(int row, int column, String newText) {
		switch(column) {
			case 2:
				AccountDTO account = accountTextField.getSelectedAccount();
				if(account != null) {
					transactionTable.setText(row, column, account.getName());
					
					//substract 1 because of the header
					int idx = row + transactionTable.getStartRow() - 1;
					importProvider.onCellChanged(statementNumber, idx, account);
				}
				break;
			default:
				break;
		}
	}

}
